package com.example.gomoku;


import android.annotation.SuppressLint;

import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.media.Image;
import android.util.Log;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.Arrays;

public class DBHelper extends SQLiteOpenHelper {
    //定义库的名称、表的名称
    public static final String DB_NAME = "gomoku";
    public static UriMatcher uriMatcher = new UriMatcher( UriMatcher.NO_MATCH );

    public static final int DB_VERSION = 1;
    public static final String TABLE_USER = "user";
    private static final String CREATE_TABLE_USER = "CREATE TABLE IF NOT EXISTS " + TABLE_USER + " (" +
            "uid TEXT PRIMARY KEY, " +
            "upwd TEXT NOT NULL, " +
            "uname TEXT NOT NULL, " +
            "uavatar BLOB, " +
            "win DOUBLE, " +
            "count INT)";
    //在构造函数中传入数据库名称、创建数据库
    public DBHelper(@Nullable Context context) {
        super( context, DB_NAME, null, DB_VERSION );//创作者自定义的版本号
    }
    //创建表格
    @Override
    public void onCreate(SQLiteDatabase db) {
        //定义一个sql语句——>执行语句
        try{
            db.execSQL( CREATE_TABLE_USER );
        }catch (SQLException e){
            e.printStackTrace();
        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public long registerUser(User user){
        SQLiteDatabase db = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put( "uid",user.uid );
        values.put( "upwd",user.upwd );
        values.put( "uname",user.uname );
        //vals.put( "uavatar", user.uavatar.toString() );
        return db.insert(TABLE_USER, null, values);
    }

    @SuppressLint("Range")
    public String verifyUser(String uid,String upwd){
        SQLiteDatabase db = getReadableDatabase();
        String whereClause = "uid=? AND upwd=?";
        Cursor cursor = db.query(TABLE_USER,null,whereClause,new String[]{uid,upwd},null,null,null);
        if (cursor.moveToFirst()) {
            String uname = cursor.getString(cursor.getColumnIndex("uname"));
            cursor.close();
            return uname;
        } else {
            cursor.close();
            return "";
        }

    }
    @SuppressLint("Range")
    public User getUserByUID(String uid) {
        SQLiteDatabase db = getReadableDatabase();
        String whereClause = "uid=?";
        Cursor cursor = db.query(TABLE_USER, null, whereClause, new String[]{uid}, null, null, null);

        if (cursor.moveToFirst()) {
            String upwd = cursor.getString(cursor.getColumnIndex("upwd"));
            String uname = cursor.getString(cursor.getColumnIndex("uname"));
            byte[] uavatar = cursor.getBlob(cursor.getColumnIndex("uavatar"));
            Log.d("DBHelper", "User found: " + uname);
            double win = cursor.getDouble(cursor.getColumnIndex("win"));
            int count = cursor.getInt(cursor.getColumnIndex("count"));
            cursor.close();
            return new User(uid, upwd, uname, uavatar);
        } else {
            Log.d("DBHelper", "No user found for UID: " + uid);
            cursor.close();
            return null;
        }
    }

    public void updatePassword(String userId, String newPassword) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("upwd", newPassword);
        db.update(TABLE_USER, values, "uid=?", new String[]{userId});
        db.close();
    }
    // 更新用户头像
    public void updateUserAvatar(String userId, byte[] avatar) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("uavatar", avatar);
        db.update(TABLE_USER, values, "uid=?", new String[]{userId});
    }
    @Nullable
    public byte[] getUserAvatar(String userId) {
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(TABLE_USER, new String[]{"uavatar"}, "uid=?", new String[]{userId}, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            @SuppressLint("Range")
            byte[] avatar = cursor.getBlob(cursor.getColumnIndex("uavatar"));
            cursor.close();
            return avatar;
        }
        return null;
    }
    public String getUname(String userId){
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.query(TABLE_USER, new String[]{"uname"}, "uid=?", new String[]{userId}, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {
            @SuppressLint("Range")
            String uname = cursor.getString(cursor.getColumnIndex("uname"));
            Log.d("DBHelper", "Fetched uname: " + uname); // 添加日志
            cursor.close();
            return uname;
        }
        if (cursor != null) {
            cursor.close();
        }
        Log.d("DBHelper", "No uname found for userId: " + userId); // 添加日志
        return null;
    }
}
